import { computed, watch } from 'vue'
import type { Website } from '@/data/config'

interface UseWebsiteFilterOptions {
  websites: Website[]
  searchQuery: string
  activeCategory: string
}

export function useWebsiteFilter(options: UseWebsiteFilterOptions) {
  const { websites, searchQuery, activeCategory } = options

  const filteredWebsites = computed(() => {
    let filtered = websites

    // 分类筛选
    if (activeCategory !== 'all') {
      filtered = filtered.filter(website => 
        website.category.includes(activeCategory)
      )
    }

    // 搜索筛选
    if (searchQuery.trim()) {
      const query = searchQuery.toLowerCase().trim()
      filtered = filtered.filter(website => 
        website.name.toLowerCase().includes(query) ||
        website.description.toLowerCase().includes(query) ||
        website.category.some(cat => cat.toLowerCase().includes(query))
      )
    }

    return filtered
  })

  const hasResults = computed(() => filteredWebsites.value.length > 0)

  return {
    filteredWebsites,
    hasResults
  }
}